#!/bin/ksh
case $# in
    0 ) print "Usage: $0 [-2] <C-File> <Top-Level-Function> ...";
        exit 1;;
    *)  case $1 in
	    -2) YAP_VERSION=2; shift; FILE=$1; shift ;;
	    *)  YAP_VERSION=3; FILE=$1; shift ;;
	esac;;
esac
#set -x

NAME=${FILE%%.*};
if [[ $FILE == ${NAME}.tar ]]
then
  FILE=${NAME}.c
fi
echo $FILE
echo $NAME
BIN=/pub/yap/tokenizers
AWK=awk
CTAGS=/usr/bin/ctags

echo >> $FILE

# remove #includes, print strings and do a little indenting so as not to
#  confuse ctags  ............
sed -e '/^#include/d' -e 's/"[^"]*"//g'  -e "s/'.'//g" -e 's/\/\*/ & /' -e 's/\*\// & /' < $FILE | $AWK -f $BIN/tokenize_C.comments.awk | /usr/bin/tr 'A-Z' 'a-z' > oo
$CTAGS -x oo | $AWK '{print $1 "  " $2;}'  > oo.tags
$AWK '
BEGIN {
    while ( getline < "oo.tags" )
	defsarray[$2] = $1; }
{
    if (++lino in defsarray)
	{
	sub(defsarray[lino], sprintf("%s  %s","  DEFUN",defsarray[lino]));
	}
    print $0
} ' oo > oo1

sed -f "$BIN"/tokenize_C.sed < oo1 | /usr/bin/tr -c 'a-zA-Z0-9{}_=+-\%?<>|!&' '\012' | sed -f "$BIN"/tokenize_C.synonyms | $BIN/Cexpand $* | $AWK -f $BIN/tokenize_C.awk version="$YAP_VERSION" > oo
case $YAP_VERSION in
    2)	mv oo $NAME.tokens;;
    3)	cat oo | wc -l > $NAME.tokens;
      	cat oo >> $NAME.tokens;
	#rm oo;;
	;;
esac
chmod g+rw $NAME.tokens
#rm oo.tags
